extends CharacterBody2D

@export var move_speed:int = 50
@onready var sprite:AnimatedSprite2D = $AnimatedSprite2D
@onready var hitbox:Area2D = $Hitbox
@onready var player:CharacterBody2D = $"/root/World/Player"
@onready var death_sound = $DeathSound

var active:bool = false
var gravity:int = 1600

func _ready():	
	hitbox.body_entered.connect(_on_body_entered)
	player.player_died.connect(_on_player_died)
	
func _process(delta):
	pass

func  _physics_process(delta):
	if not active:
		return
	velocity.x = -move_speed
	velocity.y = gravity*delta
	move_and_slide()

func set_active(value):
	active=value
	if active:
		sprite.play('walk')

func _on_body_entered(body):
	if body.is_in_group('player') and active:
		player.die()

func _on_player_died():
	set_active(false)
	sprite.play('idle')

func die():
	death_sound.play()
	sprite.play('death')
	set_active(false)
	sprite.animation_finished.connect(_on_animation_finished)
	pass

func _on_animation_finished():
	queue_free()
